package com.Earth01;

import java.util.ArrayList;
import java.util.Collections;
/*
* LeedCode  378.有序矩阵中第K小的元素
* 能过，但不是最优
* */
public class Earth240701 {
    public static void main(String[] args) {
        int[][] matrix = {{1,5,9},{10,11,13},{12,13,15}};
        int k = 8;
        int res = kthSmallest(matrix,k);
        System.out.println(res);
    }
    public static int kthSmallest(int[][] matrix, int k) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                list.add(matrix[i][j]);
            }
        }
        Collections.sort(list);
        return list.get(k-1);
    }
}
